package cn.edu.scau.cmi.huangxiyan.client;

import cn.edu.scau.cmi.huangxiyan.factoryMethod.*;
import cn.edu.scau.cmi.huangxiyan.simpleFactoryMethod.meat.*;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class FactoryMethodClient extends Application {
	public MeatFactory meatFactory = null;

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("FactoryMethodClient");
		VBox vBox = new VBox(15);
		vBox.setPadding(new Insets(15, 5, 5, 5));

		TextField textField = new TextField("请输入肉的种类（pork、beef或chicken）");
		textField.setPrefSize(500, 25);

		ChoiceBox<String> choiceBox = new ChoiceBox<String>(
				FXCollections.observableArrayList("", "pork", "beef", "chicken"));
		choiceBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
			@SuppressWarnings("rawtypes")
			public void changed(ObservableValue ov, Number value, Number new_value) {
				String[] meat = { "", "pork", "beef", "chicken" };
				textField.setText(meat[new_value.intValue()]);
			};
		});
		choiceBox.setPrefSize(100, 25);

		HBox hBox = new HBox(5);
		hBox.setPadding(new Insets(5, 0, 5, 0));
		hBox.getChildren().addAll(textField, choiceBox);

		TextArea textArea = new TextArea("");
		textArea.setPrefSize(600, 200);
		textArea.setEditable(false);

		Button button = new Button("生产");
		button.setPrefSize(200, 25);
		button.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				String meatType = textField.getText();
				meatFactory = null;

				switch (meatType) {
				case "pork":
					meatFactory = new PorkFactory();
					break;
				case "beef":
					meatFactory = new BeefFactory();
					break;
				case "chicken":
					meatFactory = new ChickenFactory();
					break;
				default:
					textArea.appendText("输入肉的种类错误，请重新输入pork、beef或chicken！\n");
					textField.setText("请输入肉的种类（pork、beef或chicken）");
				}

				if (meatFactory != null) {
					Meat meat = meatFactory.getMeat();
					textArea.appendText(meat.cook() + "\n");
				}
			}
		});
		BorderPane borderPane = new BorderPane();
		borderPane.setCenter(button);

		vBox.getChildren().addAll(hBox, textArea, borderPane);
		Scene scene = new Scene(vBox, 600, 300);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}
